﻿using System;
using Microsoft.Xna.Framework;
using ProjetCom.Model.Template;

namespace ProjetCom.Model.CasseBriques
{
    class Brique : Element
    {
        private int m_life;

        override public Boolean IsAlive
        {
            get { return m_life > 0; }
        }

        public Brique(int p_x, int p_y, int p_type)
            : base(p_x, p_y)
        {
            if (p_type < 1 || p_type > 2)
                throw new NotSupportedException(String.Format("Type de brique non supporté '{0}'.", p_type));
            else
            {
                m_life = p_type;
                m_name = "Brique" + m_life;
            }
        }

        override public void Update(Microsoft.Xna.Framework.GameTime _gameTime) { }

        override public CoteCollision EnterCollision(Element Other)
        {
            
            if (this.Rectangle.Intersects(Other.Rectangle))
            {
                Rectangle tmpRec = Rectangle.Intersect(this.Rectangle, Other.Rectangle);

                // Gauche ou Droite
                if (tmpRec.Height >= tmpRec.Width)
                {
                    if (this.Centre.Y < Other.Centre.Y)
                    {
                        return CoteCollision.right;
                    }
                    else
                    {
                        return CoteCollision.left;
                    }
                }
                    // Haut ou Bas
                else
                {
                    if (this.Centre.X < Other.Centre.X)
                    {
                        return CoteCollision.bottom;
                    }
                    else
                    {
                        return CoteCollision.top;
                    }
                }
            }
            return CoteCollision.none;
        }

        override public void Collision(CoteCollision p_cote, Boolean p_moove)
        {
            m_life--;
            IsAlive = m_life > 0;
            m_name = "Brique" + m_life;
        }
    }
}
